/*
Replication do-file for "Disaster and Political Trust: Evidence from the 2017 Mexico City Earthquake"
*/

*ssc install outreg - frmttable - distinct
*ssc install gologit2 - qsvy
*ssc install fre - fre 
*ssc install sumdist
*findit pscore // install st0026_2.pkg 
*ssc install psmatch2

clear all
set more off

global Folder "C:\XXXXXXXXXX"

cd "$Folder"

*=========================================================
**#Import data and set survey design
*=========================================================
use "$Folder/data/Final_dataset_Mexico_Earthquake.dta", clear

svyset cluster [pw=wt], strata(Regio)
svydes 

global math  "\$"
global mathlab  "\$"
global money "COL\textdollar"
global labels _cons "Constant" 

#delimit;
global labels "
1.Earthquake "Earthquake"
DC_Objective "Objective DC"
1.DC_Objective "Objective DC"
Education "Education"
Age "Age"
Sex "Male"
1.Sex "Male"
Adults_hh "Number of adults in the household"
Children_hh "Number of children in the household"
DC "Self-reported DC"
FamilyPromises "Family promises"
FamilyCompliance "Family compliance"
Family_trust  "Family trust"
Social_trust "Social trust"
Gov_construction_enforcement "Damage could not have been avoided"
";
#delimit cr

*===============================================
**#PCA
*===============================================

gen GeneralTrust=.
	pca PolitPromises PubServantsPromises PolitCompliance PubServantsCompliance
	predict  f1 
	sumdist f1,  n(4) qgp(quad)
	gen GeneralTrustCat = quad
	replace GeneralTrust=f1
	sum GeneralTrust
	gen GeneralTrust_nr = (GeneralTrust-r(min))/(r(max)-r(min))
	drop f1 quad
	
gen Performance=.
	pca Performance_political_parties Performance_Gov_Mexico_City Performance_President Performance_Mayor_or_Governor Performance_Federal_gov
	predict  f1 
	sumdist f1,  n(4) qgp(quad)
	gen DesCat = quad
	replace Performance=f1
	sum Performance
	gen Performance_nr = (Performance-r(min))/(r(max)-r(min))
	drop f1 quad
	
*=================================================
**#Figure 1: Trust before and after the earthquake
*=================================================

if 1==0{
***Panel A

preserve
foreach i in PolitPromises  PubServantsPromises  PolitCompliance PubServantsCompliance  FamilyCompliance{
replace `i'=5 if `i'>4 
replace `i'=`i'+0.4 if Earthquake==1
}


twoway (hist PolitPromises if Earthquake==0, gap(60) discrete density bfcolor(ebblue*0.3) blcolor(black) lpattern(dash) lwidth(thin)) || hist PolitPromises if Earthquake==1,  discrete density title("", color(black)) ///
 bfcolor(gs3*0.6) blcolor(gs3*0.8) lpattern(solid) lwidth(thin) ylabel(, angle(horizontal)) graphregion(color(white))  ///
xla(5.21 `" "(.) Does not know" "Does not respond" "' 4.21 `" "(4) Very" "common" "' 3.21 `" "(3) Somewhat" "common" "' 2.21 `" "(2) Not very" "common" "' 1.21 `" "(1) Not common" "at all" "', valuelabels labsize(small)) gap(60) ///
title("Politicians (568 & 1,154 obs.)", margin(0 0 0 2))  xtitle("") ylab(0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50") ytitle("Percentage of respondents (%)", height(8) size(medium)) ///
legend(off) name(g1, replace) fxsize(200) xsize(12)

 twoway (hist PubServantsPromises if Earthquake==0, gap(60) discrete density bfcolor(ebblue*0.3) blcolor(black) lpattern(dash) lwidth(thin)) || hist PubServantsPromises if Earthquake==1,  discrete density title("", color(black)) ///
 bfcolor(gs3*0.6) blcolor(gs3*0.8) lpattern(solid) lwidth(thin) ylabel(, angle(horizontal)) graphregion(color(white))  ///
xla( 5.21 `" "(.) Does not know" "Does not respond" "' 4.21 `" "(4) Very" "common" "' 3.21 `" "(3) Somewhat" "common" "' 2.21 `" "(2) Not very" "common" "' 1.21 `" "(1) Not common" "at all" "', valuelabels labsize(small)) gap(60) ///
title("Public servants (563 & 1,153 obs.)", margin(0 0 0 2))  xtitle("") ytitle("") ylab(0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50") ///
legend(off) name(g2, replace) fxsize(200) nodraw xsize(10)

graph combine g1 g2, ycommon graphregion(color(white)) title("Panel A. Fulfillment of promises", color(black) margin(0 0 0 2) size(huge))  altshrink scheme(sj) iscale(1.1) name(g1g2Promises, replace)

***Panel B

 twoway (hist PolitCompliance if Earthquake==0, gap(60) discrete density bfcolor(ebblue*0.3) blcolor(black) lpattern(dash) lwidth(thin)) || hist PolitCompliance if Earthquake==1,  discrete density title("", color(black)) ///
 bfcolor(gs3*0.6) blcolor(gs3*0.8) lpattern(solid) lwidth(thin) ylabel(, angle(horizontal)) graphregion(color(white))  ///
xla(5.21 `" "(.) Does not know" "Does not respond" "' 4.21 `" "(4) Very" "common" "' 3.21 `" "(3) Somewhat" "common" "' 2.21 `" "(2) Not very" "common" "' 1.21 `" "(1) Not common" "at all" "', valuelabels labsize(small)) gap(60) ///
title("Politicians (567 & 1,152 obs.)", margin(0 0 0 2))  xtitle("") ylab(0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50") ytitle("Percentage of respondents (%)", height(8) size(medium))  ///
legend(off) name(g1, replace) fxsize(200) nodraw 


 twoway (hist PubServantsCompliance if Earthquake==0, gap(60) discrete density bfcolor(ebblue*0.3) blcolor(black) lpattern(dash) lwidth(thin)) || hist PubServantsCompliance if Earthquake==1,  discrete density title("", color(black)) ///
 bfcolor(gs3*0.6) blcolor(gs3*0.8) lpattern(solid) lwidth(thin) ylabel(, angle(horizontal)) graphregion(color(white))  ///
xla(5.21 `" "(.) Does not know" "Does not respond" "' 4.21 `" "(4) Very" "common" "' 3.21 `" "(3) Somewhat" "common" "' 2.21 `" "(2) Not very" "common" "' 1.21 `" "(1) Not common" "at all" "', valuelabels labsize(small)) gap(60) ///
title("Public servants (568 & 1,153 obs.)", margin(0 0 0 2))  xtitle("") ytitle("") ylab(0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50") ///
legend(off) name(g2, replace) fxsize(200) nodraw

graph combine g1 g2, ycommon graphregion(color(white)) title("Panel B. Compliance with the law", color(black) margin(0 0 0 2) size(huge))  altshrink scheme(sj) iscale(1.1) name(g1g2Compliance, replace) 

graph combine g1g2Promises g1g2Compliance, graphregion(color(white)) ycommon title("", color(black) size(huge))  altshrink scheme(sj) iscale(1.2)  xsize(5) name(PromisesCompliance, replace)  cols(1) 

graph display PromisesCompliance, xsize(5)
graph export "$Folder/output/Figure_1_quake.pdf", as(pdf) replace
restore
}

*===============================================
**#Figure 2: PCA - trust before and after the earthquake
*===============================================

if 1==0{
preserve 
	kdensity GeneralTrust_nr, nograph generate(x fx)
	kdensity GeneralTrust_nr if Earthquake==0, nograph generate(fx0) at(x) 
	kdensity GeneralTrust_nr if Earthquake==1, nograph generate(fx1) at(x) 
	label var fx0 "Pre"
	label var fx1 "Post"
	line fx0 fx1 x, sort ytitle(Density) xtitle("") scheme(s2mono) graphregion(fcolor(white))
	graph export "$Folder/output/Figure_2_quake.pdf", as(pdf) replace
restore 
}

*==============================================================
**#Table 1: Effect of the Earthquake on General Political Trust
*==============================================================

global Controls1 Age Sex Education Adults_hh Children_hh i.Region 
global Controls2 Age Sex Education Adults_hh Children_hh i.Region PRI_pre PRD_pre
global Controls3 Age Sex Education Adults_hh Children_hh i.Region Wealth_pre
global Controls4 Age Sex Education Adults_hh Children_hh i.Region Risk_pre Patience_pre
global Controls5 Age Sex Education Adults_hh Children_hh i.Region Social_trust_pre
global Controls6 Age Sex Education Adults_hh Children_hh i.Region PRI_pre PRD_pre Wealth_pre Risk_pre Patience_pre Social_trust_pre

if 1==0{
svy: reg GeneralTrust_nr i.Earthquake $Controls1
est sto m1 
svy: reg GeneralTrust_nr i.Earthquake $Controls2 
est sto m2
svy: reg GeneralTrust_nr i.Earthquake $Controls3
est sto m3
svy: reg GeneralTrust_nr i.Earthquake $Controls4
est sto m4
svy: reg GeneralTrust_nr i.Earthquake $Controls5
est sto m5
svy: reg GeneralTrust_nr i.Earthquake $Controls6
est sto m6

esttab m1 m2 m3 m4 m5 m6 using "$Folder/output/Table_1_quake.tex", ///
	coeflabels($labels) refcat(1.Earthquake   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace keep(1.Earthquake _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///
	stats(N r2,  labels(`"Observations"' `"R-squared"', prefix(\addlinespace[3pt])) fmt( %10.0fc %10.3fc 0 0) ) ///
	title(Marginal Effects of Earthquake on General Political Trust\label{tab:earthquaketrust})  ///
	nonotes ///
	addnote("Clustered standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}

*============================================================================================
**#Table 2: Association between Self-reported Distribution Centers and General Political Trust
*============================================================================================

if 1==0{
svy: reg GeneralTrust_nr DC $Controls1 
est sto m1 
svy: reg GeneralTrust_nr DC $Controls2 
est sto m2
svy: reg GeneralTrust_nr DC $Controls3
est sto m3
svy: reg GeneralTrust_nr DC $Controls4
est sto m4
svy: reg GeneralTrust_nr DC $Controls5
est sto m5
svy: reg GeneralTrust_nr DC $Controls6
est sto m6

esttab m1 m2 m3 m4 m5 m6 using "$Folder/output/Table_2_quake.tex", ///
	coeflabels($labels) refcat(DC   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace keep(DC _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///
	stats(N r2,  labels(`"Observations"' `"R-squared"', prefix(\addlinespace[3pt])) fmt( %10.0fc %10.3fc 0 0) )  ///
	title(Marginal Effects of Earthquake on General Political Trust\label{tab:earthquaketrust})  ///
	nonotes ///
	addnote("Clustered standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}
	
*===========================================================================================
**#Table 3: Average Time and Distance between the Closest Distribution Center and Households
*===========================================================================================

if 1==0{
mat T = J(2,7,.)

sum MinTime1 if DC_Objective==1 & Earthquake==1   & City==1, detail
mat T[1,1] = r(mean)
mat T[1,2] = r(sd)
mat T[1,3] = r(min)
mat T[1,4] = r(p25)
mat T[1,5] = r(p50)
mat T[1,6] = r(p75)
mat T[1,7] = r(max)

sum WALKdistance1 if DC_Objective==1 & Earthquake==1   & City==1, detail
mat T[2,1] = r(mean)/1000
mat T[2,2] = r(sd)/1000
mat T[2,3] = r(min)/1000
mat T[2,4] = r(p25)/1000
mat T[2,5] = r(p50)/1000
mat T[2,6] = r(p75)/1000
mat T[2,7] = r(max)/1000

matlist T

mat rownames T = "Walking time (minutes)" "Distance (Km)"

frmttable using "$Folder/output/Table_3_quake",  statmat(T) tex varlabels replace ///
	ctitle("", Mean, S.D., Min, "p25", Median, p75, Max) sdec(2)
}	

*=========================================================================================
**#Table 4: Association between Objective Distribution Centers and General Political Trust
*=========================================================================================

if 1==0{
svy: reg GeneralTrust_nr i.DC_Objective  $Controls1 if City==1 & Earthquake==1
est sto m1 
svy: reg GeneralTrust_nr i.DC_Objective  $Controls2 if City==1 & Earthquake==1
est sto m2
svy: reg GeneralTrust_nr i.DC_Objective  $Controls3 if City==1 & Earthquake==1
est sto m3
svy: reg GeneralTrust_nr i.DC_Objective  $Controls4 if City==1 & Earthquake==1
est sto m4
svy: reg GeneralTrust_nr i.DC_Objective  $Controls5 if City==1 & Earthquake==1
est sto m5
svy: reg GeneralTrust_nr i.DC_Objective  $Controls6 if City==1 & Earthquake==1
est sto m6

esttab m1 m2 m3 m4 m5 m6 using "$Folder/output/Table_4_quake.tex", ///
	coeflabels($labels) refcat(1.DC_Objective   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace keep(1.DC_Objective _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///
	stats(N r2,  labels(`"Observations"' `"R-squared"', prefix(\addlinespace[3pt])) fmt( %10.0fc %10.3fc 0 0) )  ///
	title(Marginal Effects of Earthquake on General Political Trust\label{tab:earthquaketrust})  ///
	nonotes ///
	addnote("Clustered standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}

*======================================================	
**#Table 5: Effect of the Earthquake on other variables - Potentially mediated by the decrease in trust
*======================================================

if 1==0{
cls
foreach var in PRD Taxing_more_rich Revenue_for_poorest Security Taxes_security Trust_gov_reports {
fre `var'
qui	svy: reg `var' i.Earthquake $Controls6
est sto `var'
}

foreach var in Security Taxes_security  {
	fre `var' if `var'!=2
qui	svy: reg `var' i.Earthquake $Controls6 if `var'!=2
est sto `var'
}

esttab PRD Taxing_more_rich Revenue_for_poorest Security Taxes_security Trust_gov_reports using "$Folder/output/Table_5_quake.tex", ///
	coeflabels($labels) refcat(1.Earthquake   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace order(Gov_construction_enforcement DC) keep(1.Earthquake _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///
	stats(N r2,  labels(`"Observations"' `"R-squared"', prefix(\addlinespace[3pt])) fmt( %10.0fc %10.3fc 0 0) ) ///
	title(Effect of the Earthquake on other outcome variables)  ///
	nonotes ///
	addnote("Standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}
	
*======================================================================================
**#Table B.2: Survey design
*======================================================================================

if 1==0{
**All municipalities
distinct Municipality if Region==101 // CENTER 4
distinct Municipality if Region==104  // NORTH 9 
distinct Municipality if Region==102  // SOUTH 6
distinct Municipality if Region==105  // EAST 6
distinct Municipality if Region==103 // WEST 4

**Pre-earthquake: municipalities
distinct Municipality if Region==101 & Earthquake==0 // CENTER 4
distinct Municipality if Region==104 & Earthquake==0 // NORTH 9
distinct Municipality if Region==102 & Earthquake==0 // SOUTH 6 
distinct Municipality if Region==105 & Earthquake==0 // EAST 6 
distinct Municipality if Region==103 & Earthquake==0 // WEST 4

**Pre-earthquake: SSUs
distinct cluster if Region==101 & Earthquake==0 // 19 (109)
distinct cluster if Region==104 & Earthquake==0 // 19 (111)
distinct cluster if Region==102 & Earthquake==0 // 13 (78)
distinct cluster if Region==105 & Earthquake==0 // 36 (213)
distinct cluster if Region==103 & Earthquake==0 // 10 (58)
distinct cluster if  Earthquake==0

**Post-earthquake: municipalities
distinct Municipality if Region==101 & Earthquake==1 // CENTER 4
distinct Municipality if Region==104 & Earthquake==1 // NORTH 9
distinct Municipality if Region==102 & Earthquake==1 // SOUTH 6 
distinct Municipality if Region==105 & Earthquake==1 // EAST 6 
distinct Municipality if Region==103 & Earthquake==1 // WEST 4

**Post-earthquake: SSUs
distinct cluster if Region==101 & Earthquake==1 // 32 (229)
distinct cluster if Region==104 & Earthquake==1 // 24 (229)
distinct cluster if Region==102 & Earthquake==1 // 26 (156)
distinct cluster if Region==105 & Earthquake==1 // 62 (431)
distinct cluster if Region==103 & Earthquake==1 // 18 (119)
distinct cluster if  Earthquake==1 // 162
}

*==============================================
**#Table C.4: Descriptive statistics
*==============================================

if 1==0{
tab Region, gen(RegionOriginal)
mat T = J(17,8,.)

ttest Sex, by(Earthquake)
mat T[1,1] = r(mu_1)
mat T[1,2] = r(sd_1)
mat T[1,3] = r(mu_2)
mat T[1,4] = r(sd_2)
mat T[1,5] = r(mu_1) - r(mu_2)
mat T[1,6] = r(p)
mat T[1,7] = r(N_1)
mat T[1,8] = r(N_2)
mat list T

ttest Age, by(Earthquake)
mat T[2,1] = r(mu_1)
mat T[2,2] = r(sd_1)
mat T[2,3] = r(mu_2)
mat T[2,4] = r(sd_2)
mat T[2,5] = r(mu_1) - r(mu_2)
mat T[2,6] = r(p)
mat T[2,7] = r(N_1)
mat T[2,8] = r(N_2)
 
ttest Education, by(Earthquake)
mat T[3,1] = r(mu_1)
mat T[3,2] = r(sd_1)
mat T[3,3] = r(mu_2)
mat T[3,4] = r(sd_2)
mat T[3,5] = r(mu_1) - r(mu_2)
mat T[3,6] = r(p)
mat T[3,7] = r(N_1)
mat T[3,8] = r(N_2)
 
ttest Adults_hh, by(Earthquake)
mat T[4,1] = r(mu_1)
mat T[4,2] = r(sd_1)
mat T[4,3] = r(mu_2)
mat T[4,4] = r(sd_2)
mat T[4,5] = r(mu_1) - r(mu_2)
mat T[4,6] = r(p)
mat T[4,7] = r(N_1)
mat T[4,8] = r(N_2)

ttest Children_hh, by(Earthquake)
mat T[5,1] = r(mu_1)
mat T[5,2] = r(sd_1)
mat T[5,3] = r(mu_2)
mat T[5,4] = r(sd_2)
mat T[5,5] = r(mu_1) - r(mu_2)
mat T[5,6] = r(p)
mat T[5,7] = r(N_1)
mat T[5,8] = r(N_2)

ttest PRI, by(Earthquake)
mat T[6,1] = r(mu_1)
mat T[6,2] = r(sd_1)
mat T[6,3] = r(mu_2)
mat T[6,4] = r(sd_2)
mat T[6,5] = r(mu_1) - r(mu_2)
mat T[6,6] = r(p)
mat T[6,7] = r(N_1)
mat T[6,8] = r(N_2)

ttest PRD, by(Earthquake)
mat T[7,1] = r(mu_1)
mat T[7,2] = r(sd_1)
mat T[7,3] = r(mu_2)
mat T[7,4] = r(sd_2)
mat T[7,5] = r(mu_1) - r(mu_2)
mat T[7,6] = r(p)
mat T[7,7] = r(N_1)
mat T[7,8] = r(N_2)

ttest Wealth, by(Earthquake)
mat T[8,1] = r(mu_1)
mat T[8,2] = r(sd_1)
mat T[8,3] = r(mu_2)
mat T[8,4] = r(sd_2)
mat T[8,5] = r(mu_1) - r(mu_2)
mat T[8,6] = r(p)
mat T[8,7] = r(N_1)
mat T[8,8] = r(N_2)

ttest Social_trust, by(Earthquake)
mat T[9,1] = r(mu_1)
mat T[9,2] = r(sd_1)
mat T[9,3] = r(mu_2)
mat T[9,4] = r(sd_2)
mat T[9,5] = r(mu_1) - r(mu_2)
mat T[9,6] = r(p)
mat T[9,7] = r(N_1)
mat T[9,8] = r(N_2)

ttest RegionOriginal1, by(Earthquake)
mat T[10,1] = r(mu_1)
mat T[10,2] = r(sd_1)
mat T[10,3] = r(mu_2)
mat T[10,4] = r(sd_2)
mat T[10,5] = r(mu_1) - r(mu_2)
mat T[10,6] = r(p)
mat T[10,7] = r(N_1)
mat T[10,8] = r(N_2)

ttest RegionOriginal4, by(Earthquake)
mat T[11,1] = r(mu_1)
mat T[11,2] = r(sd_1)
mat T[11,3] = r(mu_2)
mat T[11,4] = r(sd_2)
mat T[11,5] = r(mu_1) - r(mu_2)
mat T[11,6] = r(p)
mat T[11,7] = r(N_1)
mat T[11,8] = r(N_2)

ttest RegionOriginal2, by(Earthquake)
mat T[12,1] = r(mu_1)
mat T[12,2] = r(sd_1)
mat T[12,3] = r(mu_2)
mat T[12,4] = r(sd_2)
mat T[12,5] = r(mu_1) - r(mu_2)
mat T[12,6] = r(p)
mat T[12,7] = r(N_1)
mat T[12,8] = r(N_2)

ttest RegionOriginal5, by(Earthquake)
mat T[13,1] = r(mu_1)
mat T[13,2] = r(sd_1)
mat T[13,3] = r(mu_2)
mat T[13,4] = r(sd_2)
mat T[13,5] = r(mu_1) - r(mu_2)
mat T[13,6] = r(p)
mat T[13,7] = r(N_1)
mat T[13,8] = r(N_2)

ttest RegionOriginal3, by(Earthquake)
mat T[14,1] = r(mu_1)
mat T[14,2] = r(sd_1)
mat T[14,3] = r(mu_2)
mat T[14,4] = r(sd_2)
mat T[14,5] = r(mu_1) - r(mu_2)
mat T[14,6] = r(p)
mat T[14,7] = r(N_1)
mat T[14,8] = r(N_2)

ttest Risk_aversion, by(Earthquake)
mat T[15,1] = r(mu_1)
mat T[15,2] = r(sd_1)
mat T[15,3] = r(mu_2)
mat T[15,4] = r(sd_2)
mat T[15,5] = r(mu_1) - r(mu_2)
mat T[15,6] = r(p)
mat T[15,7] = r(N_1)
mat T[15,8] = r(N_2)

ttest Patience, by(Earthquake)
mat T[16,1] = r(mu_1)
mat T[16,2] = r(sd_1)
mat T[16,3] = r(mu_2)
mat T[16,4] = r(sd_2)
mat T[16,5] = r(mu_1) - r(mu_2)
mat T[16,6] = r(p)
mat T[16,7] = r(N_1)
mat T[16,8] = r(N_2)

ttest GeneralTrust_nr, by(Earthquake)
mat T[17,1] = r(mu_1)
mat T[17,2] = r(sd_1)
mat T[17,3] = r(mu_2)
mat T[17,4] = r(sd_2)
mat T[17,5] = r(mu_1) - r(mu_2)
mat T[17,6] = r(p)
mat T[17,7] = r(N_1)
mat T[17,8] = r(N_2)

matlist T

mat rownames T = Sex Age Education Adults Children PRI PRD Wealth Social_Trust Center North South East  West Risk_Aversion Patience General_Trust

frmttable using "$Folder/output/Table_C4_quake",  statmat(T) tex replace ///
	ctitle("", Mean, s.d., Mean, s.d., Difference, "(p-value)", Npre, Npost) sdec(3)
 
*Footnote: 
 codebook Sex Age Education Adults_hh Children_hh PRI PRD Wealth Social_trust Region Risk_aversion Patience GeneralTrust_nr
}
 
*================================================
**#Table D.1: Distribution of dependent variables 
**===============================================

if 1==0{
mat T = J(15,13.,.)

foreach number in 1 2 3 4 {
count if Earthquake==0 & PolitPromises==`number'
mat T[`number',1] = r(N)
mat T[`number',2] = r(N)/568*100
count if Earthquake==1 & PolitPromises==`number'
mat T[`number',3] = r(N)
mat T[`number',4] = r(N)/1154*100
count if PolitPromises==`number'
mat T[`number',5] = r(N)
mat T[`number',6] = r(N)/1722*100
count if Earthquake==0 & PubServantsPromises==`number'
mat T[`number',7] = r(N)
mat T[`number',8] = r(N)/563*100
count if Earthquake==1 & PubServantsPromises==`number'
mat T[`number',9] = r(N)
mat T[`number',10] = r(N)/1153*100
count if PubServantsPromises==`number'
mat T[`number',11] = r(N)
mat T[`number',12] = r(N)/1716*100
count if Earthquake==0 & PolitCompliance==`number'
mat T[`number'+7,1] = r(N)
mat T[`number'+7,2] = r(N)/567*100
count if Earthquake==1 & PolitCompliance==`number'
mat T[`number'+7,3] = r(N)
mat T[`number'+7,4] = r(N)/1152*100
count if PolitCompliance==`number'
mat T[`number'+7,5] = r(N)
mat T[`number'+7,6] = r(N)/1719*100
count if Earthquake==0 & PubServantsCompliance==`number'
mat T[`number'+7,7] = r(N)
mat T[`number'+7,8] = r(N)/568*100
count if Earthquake==1 & PubServantsCompliance==`number'
mat T[`number'+7,9] = r(N)
mat T[`number'+7,10] = r(N)/1153*100
count if PubServantsCompliance==`number'
mat T[`number'+7,11] = r(N)
mat T[`number'+7,12] = r(N)/1721*100
}
count if Earthquake==0 & PolitPromises!=.
mat T[5,1] = r(N)
mat T[5,2] = r(N)/568*100
count if Earthquake==1 & PolitPromises!=.
mat T[5,3] = r(N)
mat T[5,4] = r(N)/1154*100
count if  PolitPromises!=.
mat T[5,5] = r(N)
mat T[5,6] = r(N)/1722*100
tab PolitPromises Earthquake, chi2
mat T[6,5] = r(chi2)
mat T[6,6] = r(p)
matlist T

count if Earthquake==0 & PubServantsPromises!=.
mat T[5,7] = r(N)
mat T[5,8] = r(N)/563*100
count if Earthquake==1 & PubServantsPromises!=.
mat T[5,9] = r(N)
mat T[5,10] = r(N)/1153*100
count if  PubServantsPromises!=.
mat T[5,11] = r(N)
mat T[5,12] = r(N)/1716*100
tab PubServantsPromises Earthquake, chi2
mat T[6,11] = r(chi2)
mat T[6,12] = r(p)

count if Earthquake==0 & PolitCompliance!=.
mat T[5+7,1] = r(N)
mat T[5+7,2] = r(N)/567*100
count if Earthquake==1 & PolitCompliance!=.
mat T[5+7,3] = r(N)
mat T[5+7,4] = r(N)/1152*100
count if  PolitCompliance!=.
mat T[5+7,5] = r(N)
mat T[5+7,6] = r(N)/1719*100
tab PolitCompliance Earthquake, chi2
mat T[6+7,5] = r(chi2)
mat T[6+7,6] = r(p)
matlist T

count if Earthquake==0 & PubServantsCompliance!=.
mat T[5+7,7] = r(N)
mat T[5+7,8] = r(N)/568*100
count if Earthquake==1 & PubServantsCompliance!=.
mat T[5+7,9] = r(N)
mat T[5+7,10] = r(N)/1153*100
count if  PubServantsCompliance!=.
mat T[5+7,11] = r(N)
mat T[5+7,12] = r(N)/1721*100
tab PubServantsCompliance Earthquake, chi2
mat T[6+7,11] = r(chi2)
mat T[6+7,12] = r(p)

matlist T

mat rownames T = "Not common at all" "Not very common" "Somewhat common" "Very common" "Total" "chi2 and p-value" "-" ///
"Not common at all" "Not very common" "Somewhat common" "Very common" "Total" "chi2 and p-value" "-" 

frmttable using "$Folder/output/Table_D1_quake",  statmat(T) tex varlabels replace ///
	ctitle("", Pre, %, Post, %,  Total, %, Pre, %, Post, %,  Total, %) sdec(3)
}

*================================================
**#Table D.2: PCA Components
**===============================================
pca PolitPromises PubServantsPromises PolitCompliance PubServantsCompliance
/*
 --------------------------------------------------------------------------
       Component |   Eigenvalue   Difference         Proportion   Cumulative
    -------------+------------------------------------------------------------
           Comp1 |      2.29526      1.48077             0.5738       0.5738
           Comp2 |      .814494      .327238             0.2036       0.7774
           Comp3 |      .487256     .0842658             0.1218       0.8993
           Comp4 |       .40299            .             0.1007       1.0000
    --------------------------------------------------------------------------
*/

*================================================
**#Table D.3: Loadings
**===============================================
estat loadings

/*
--------------------------------------------------------------------
        Variable |    Comp1     Comp2     Comp3     Comp4 | Unexplained 
    -------------+----------------------------------------+-------------
    PolitPromi~s |   0.4828    0.5517    0.6024    0.3155 |           0 
    PubServant~s |   0.5004    0.4634   -0.6426   -0.3492 |           0 
    PolitCompl~e |   0.5061   -0.4958    0.3649   -0.6041 |           0 
    PubServant~e |   0.5102   -0.4848   -0.3018    0.6431 |           0 
    --------------------------------------------------------------------
*/

*=================================================
**#Table E.1: Targeting Self-reported Distribution Centers
*=================================================

if 1==0{
mat T = J(12,7,.)
tab Interruptions_water DC, chi2 matcell(x)
matrix list x
mat T[1,1] = x[1,1]
mat T[1,2] = x[1,2]
mat T[3,1] = x[2,1]
mat T[3,2] = x[2,2]
mat T[1,3] = x[1,1] + x[1,2]
mat T[3,3] = x[2,1] + x[2,2]
mat T[5,1] = x[1,1] + x[2,1]
mat T[5,2] = x[1,2] + x[2,2]
mat T[5,3] = T[1,3] + T[3,3]
mat T[2,1] = T[1,1]/T[1,3]*100
mat T[2,2] = T[1,2]/T[1,3]*100
mat T[2,3] = 100
mat T[4,1] = T[3,1]/T[3,3]*100
mat T[4,2] = T[3,2]/T[3,3]*100
mat T[4,3] = 100
mat T[6,1] = T[5,1]/T[5,3]*100
mat T[6,2] = T[5,2]/T[5,3]*100
mat T[6,3] = 100
mat T[7,1] = r(chi2)
mat T[8,1] = r(p)

tab Difficult_food DC, chi2 matcell(x)
matrix list x
mat T[1,5] = x[1,1]
mat T[1,6] = x[1,2]
mat T[3,5] = x[2,1]
mat T[3,6] = x[2,2]
mat T[1,7] = x[1,1] + x[1,2]
mat T[3,7] = x[2,1] + x[2,2]
mat T[5,5] = x[1,1] + x[2,1]
mat T[5,6] = x[1,2] + x[2,2]
mat T[5,7] = T[1,7] + T[3,7]
mat T[2,5] = T[1,5]/T[1,7]*100
mat T[2,6] = T[1,6]/T[1,7]*100
mat T[2,7] = 100
mat T[4,5] = T[3,5]/T[3,7]*100
mat T[4,6] = T[3,6]/T[3,7]*100
mat T[4,7] = 100
mat T[6,5] = T[5,5]/T[5,7]*100
mat T[6,6] = T[5,6]/T[5,7]*100
mat T[6,7] = 100
mat T[7,5] = r(chi2)
mat T[8,5] = r(p)

summarize PRI_pre if DC == 0
mat T[10,1] = r(mean)
mat T[11,1] = r(N)
summarize PRI_pre if DC == 1
mat T[10,2] = r(mean)
mat T[11,2] = r(N)
summarize PRI_pre if DC !=.
mat T[10,3] = r(mean)
mat T[11,3] = r(N)

tab PRI_pre DC, chi 
mat T[12,1] = r(chi2)
mat T[12,2] = r(p) 

summarize PRD_pre if DC == 0
mat T[10,5] = r(mean)
mat T[11,5] = r(N)
summarize PRD_pre if DC == 1
mat T[10,6] = r(mean)
mat T[11,6] = r(N)
summarize PRD_pre if DC != . 
mat T[10,7] = r(mean)
mat T[11,7] = r(N)

tab PRD_pre DC, chi 
mat T[12,5] = r(chi2)
mat T[12,6] = r(p) 

mat list T

mat rownames T = No "\%" Yes "\%" Total  "\%" "Pearson chi2(1)" "p-value" " " "Mean" "Total" "Test"

frmttable using "$Folder/output/Table_E1_quake",  statmat(T) tex varlabels replace ///
	ctitle("", No, Yes, Total, "", No, Yes, Total, "", No, Yes, Total, "") sdec(2)
}

*=================================================
**#Table E.2: Targeting Observed Distribution Centers
*=================================================

if 1==0{

preserve
keep if City==1
mat T = J(12,7,.)
tab Interruptions_water DC_Objective , chi2 matcell(x)
matrix list x
mat T[1,1] = x[1,1]
mat T[1,2] = x[1,2]
mat T[3,1] = x[2,1]
mat T[3,2] = x[2,2]
mat T[1,3] = x[1,1] + x[1,2]
mat T[3,3] = x[2,1] + x[2,2]
mat T[5,1] = x[1,1] + x[2,1]
mat T[5,2] = x[1,2] + x[2,2]
mat T[5,3] = T[1,3] + T[3,3]
mat T[2,1] = T[1,1]/T[1,3]*100
mat T[2,2] = T[1,2]/T[1,3]*100
mat T[2,3] = 100
mat T[4,1] = T[3,1]/T[3,3]*100
mat T[4,2] = T[3,2]/T[3,3]*100
mat T[4,3] = 100
mat T[6,1] = T[5,1]/T[5,3]*100
mat T[6,2] = T[5,2]/T[5,3]*100
mat T[6,3] = 100
mat T[7,1] = r(chi2)
mat T[8,1] = r(p)

tab Difficult_food DC_Objective , chi2 matcell(x)
matrix list x
mat T[1,5] = x[1,1]
mat T[1,6] = x[1,2]
mat T[3,5] = x[2,1]
mat T[3,6] = x[2,2]
mat T[1,7] = x[1,1] + x[1,2]
mat T[3,7] = x[2,1] + x[2,2]
mat T[5,5] = x[1,1] + x[2,1]
mat T[5,6] = x[1,2] + x[2,2]
mat T[5,7] = T[1,7] + T[3,7]
mat T[2,5] = T[1,5]/T[1,7]*100
mat T[2,6] = T[1,6]/T[1,7]*100
mat T[2,7] = 100
mat T[4,5] = T[3,5]/T[3,7]*100
mat T[4,6] = T[3,6]/T[3,7]*100
mat T[4,7] = 100
mat T[6,5] = T[5,5]/T[5,7]*100
mat T[6,6] = T[5,6]/T[5,7]*100
mat T[6,7] = 100
mat T[7,5] = r(chi2)
mat T[8,5] = r(p)

summarize PRI_pre if DC_Objective  == 0
mat T[10,1] = r(mean)
mat T[11,1] = r(N)
summarize PRI_pre if DC_Objective  == 1
mat T[10,2] = r(mean)
mat T[11,2] = r(N)
summarize PRI_pre if DC_Objective  !=.
mat T[10,3] = r(mean)
mat T[11,3] = r(N)

tab PRI_pre DC_Objective if City==1, chi 
mat T[12,1] = r(chi2)
mat T[12,2] = r(p) 

summarize PRD_pre if DC_Objective  == 0
mat T[10,5] = r(mean)
mat T[11,5] = r(N)
summarize PRD_pre if DC_Objective  == 1
mat T[10,6] = r(mean)
mat T[11,6] = r(N)
summarize PRD_pre if DC_Objective  != . 
mat T[10,7] = r(mean)
mat T[11,7] = r(N)


tab PRD_pre DC_Objective if City==1, chi 
mat T[12,5] = r(chi2)
mat T[12,6] = r(p) 

mat list T

mat rownames T = No "\%" Yes "\%" Total  "\%" "Pearson chi2(1)" "p-value" " " "Mean" "Total" "Test"

frmttable using "$Folder/output/Table_E2_quake",  statmat(T) tex varlabels replace ///
	ctitle("", No, Yes, Total, "", No, Yes, Total, "", No, Yes, Total, "") sdec(3)
restore
}

*======================================================	
**#Table E3: Government Enforcement, Distribution Centers and Performance
*======================================================

* People who think the damage could have been avoided, trust less. 65% think it could have been avoided. 

if 1==0{
svy: reg DC Gov_construction_enforcement $Controls6
est sto m2

svy: reg Performance_nr DC $Controls6
est sto m3

esttab m2 m3 using "$Folder/output/Table_E3_quake.tex", ///
	coeflabels($labels) refcat(Gov_construction_enforcement   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace order(Gov_construction_enforcement DC) keep(Gov_construction_enforcement DC _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///
	stats(N r2,  labels(`"Observations"' `"R-squared"', prefix(\addlinespace[3pt])) fmt( %10.0fc %10.3fc 0 0) ) ///
	title(Effect of the Earthquake on other outcome variables - Trust \label{tab:earthquaketrust})  ///
	nonotes ///
	addnote("Standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}

*=====================================
**# Table F.1: Matching and Weighting
*=====================================
cls 
*=======================================
******IPWRA
*=======================================
if 1==0{
mat T = J(12,12,.)
teffects ipwra (GeneralTrust_nr $Controls1)(Earthquake $Controls1), vce(cluster cluster)
matrix Table=r(table)
mat T[1,1] = Table[1,1]
mat T[1,2] = Table[2,1]
mat T[2,1] = Table[1,2]
mat T[2,2] = Table[2,2]
mat T[1,9] = e(N)

teffects ipwra (GeneralTrust_nr $Controls2)(Earthquake $Controls2), vce(cluster cluster)
matrix Table=r(table)
mat T[3,1] = Table[1,1]
mat T[3,2] = Table[2,1]
mat T[4,1] = Table[1,2]
mat T[4,2] = Table[2,2]
mat T[3,9] = e(N)
mat list T 
teffects ipwra (GeneralTrust_nr $Controls3)(Earthquake $Controls3), vce(cluster cluster)
matrix Table=r(table)
mat T[5,1] = Table[1,1]
mat T[5,2] = Table[2,1]
mat T[6,1] = Table[1,2]
mat T[6,2] = Table[2,2]
mat T[5,9] = e(N)
mat list T 
teffects ipwra (GeneralTrust_nr $Controls4)(Earthquake $Controls4), vce(cluster cluster)
matrix Table=r(table)
mat T[7,1] = Table[1,1]
mat T[7,2] = Table[2,1]
mat T[8,1] = Table[1,2]
mat T[8,2] = Table[2,2]
mat T[7,9] = e(N)
mat list T 
teffects ipwra (GeneralTrust_nr $Controls5)(Earthquake $Controls5), vce(cluster cluster)
matrix Table=r(table)
mat T[9,1] = Table[1,1]
mat T[9,2] = Table[2,1]
mat T[10,1] = Table[1,2]
mat T[10,2] = Table[2,2]
mat T[9,9] = e(N)
teffects ipwra (GeneralTrust_nr $Controls6)(Earthquake $Controls6), vce(cluster cluster)
matrix Table=r(table)
mat T[11,1] = Table[1,1]
mat T[11,2] = Table[2,1]
mat T[12,1] = Table[1,2]
mat T[12,2] = Table[2,2]
mat T[11,9] = e(N)
mat list T 

*=======================================
*****NNMATCH
*=======================================
teffects nnmatch (GeneralTrust_nr $Controls1)(Earthquake), ematch(i.Sex i.Region) biasadj(Age Education Adults_hh Children_hh) nneighbor(4) vce(robust, nn(4))
matrix Table=r(table)
mat T[1,3] = Table[1,1]
mat T[1,4] = Table[2,1]
mat list T 

teffects nnmatch (GeneralTrust_nr $Controls2)(Earthquake), ematch(i.Sex i.Region) biasadj(Age Education Adults_hh Children_hh PRI_pre PRD_pre) nneighbor(4) vce(robust, nn(4))
matrix Table=r(table)
mat T[3,3] = Table[1,1]
mat T[3,4] = Table[2,1]
mat list T 

teffects nnmatch (GeneralTrust_nr $Controls3)(Earthquake), ematch(i.Sex i.Region) biasadj(Age Education Adults_hh Children_hh Wealth_pre) nneighbor(4) vce(robust, nn(4))
matrix Table=r(table)
mat T[5,3] = Table[1,1]
mat T[5,4] = Table[2,1]
mat list T 

teffects nnmatch (GeneralTrust_nr $Controls4)(Earthquake), ematch(i.Sex i.Region) biasadj(Age Education Adults_hh Children_hh Risk_pre Patience_pre) nneighbor(4) vce(robust, nn(4))
matrix Table=r(table)
mat T[7,3] = Table[1,1]
mat T[7,4] = Table[2,1]
mat list T 

teffects nnmatch (GeneralTrust_nr $Controls5)(Earthquake), ematch(i.Sex i.Region) biasadj(Age Education Adults_hh Children_hh Social_trust_pre) nneighbor(4) vce(robust, nn(4))
matrix Table=r(table)
mat T[9,3] = Table[1,1]
mat T[9,4] = Table[2,1]
mat list T 

teffects nnmatch (GeneralTrust_nr $Controls6)(Earthquake), ematch(i.Sex i.Region) biasadj(Age Education Adults_hh Children_hh PRI_pre PRD_pre Risk_pre  Wealth_pre Risk_pre Patience_pre Social_trust_pre) nneighbor(4) vce(robust, nn(4))
matrix Table=r(table)
mat T[11,3] = Table[1,1]
mat T[11,4] = Table[2,1]
mat list T 

*=======================================
**Propensity-score matching (PSM) 
*=======================================

teffects psmatch (GeneralTrust_nr)(Earthquake $Controls1, logit), ate nneighbor(4) vce(robust, nn(4))  
matrix Table=r(table)
mat T[1,5] = Table[1,1]
mat T[1,6] = Table[2,1]
mat list T 

teffects psmatch (GeneralTrust_nr)(Earthquake $Controls2, logit), ate nneighbor(4) vce(robust, nn(4))  
matrix Table=r(table)
mat T[3,5] = Table[1,1]
mat T[3,6] = Table[2,1]
mat list T 

teffects psmatch (GeneralTrust_nr)(Earthquake $Controls3, logit), ate nneighbor(4) vce(robust, nn(4))  
matrix Table=r(table)
mat T[5,5] = Table[1,1]
mat T[5,6] = Table[2,1]
mat list T 

teffects psmatch (GeneralTrust_nr)(Earthquake $Controls4, logit), ate nneighbor(4) vce(robust, nn(4))  
matrix Table=r(table)
mat T[7,5] = Table[1,1]
mat T[7,6] = Table[2,1]
mat list T 

teffects psmatch (GeneralTrust_nr)(Earthquake $Controls5, logit), ate nneighbor(4) vce(robust, nn(4))  
matrix Table=r(table)
mat T[9,5] = Table[1,1]
mat T[9,6] = Table[2,1]
mat list T 

teffects psmatch (GeneralTrust_nr)(Earthquake $Controls6, logit), ate nneighbor(4) vce(robust, nn(4))  
matrix Table=r(table)
mat T[11,5] = Table[1,1]
mat T[11,6] = Table[2,1]
mat list T 

*=======================================
*** KERNEL matching:
*=======================================
	
gen Miss=1 if GeneralTrust_nr==.

*1) CREATE PSCORE CONTROLS1
xi: pscore Earthquake $Controls1 if Miss!=1, pscore(psGeneralTrust_nr1) blockid(mbGeneralTrust_nr1) logit detail

*2) ****NO REPLACEMENT
psmatch2 Earthquake, outcome(GeneralTrust_nr) pscore(psGeneralTrust_nr1) noreplacement ate // 

*3) WITH REPLACEMENT, 4 NEIGHBORS
gen regscore1 = ln(psGeneralTrust_nr1/(1-psGeneralTrust_nr1))  // ols function of the pscore
sum regscore1
*return list
global caliper =  `r(sd)'*0.2
display $caliper 

*1) CREATE PSCORE CONTROLS2
xi: pscore Earthquake $Controls2 if Miss!=1, pscore(psGeneralTrust_nr2) blockid(mbGeneralTrust_nr2) logit detail

*2) ****NO REPLACEMENT
psmatch2 Earthquake, outcome(GeneralTrust_nr) pscore(psGeneralTrust_nr2) noreplacement ate // 

*3) WITH REPLACEMENT, 4 NEIGHBORS
gen regscore2 = ln(psGeneralTrust_nr2/(1-psGeneralTrust_nr2))  // ols function of the pscore
sum regscore2
*return list
global caliper =  `r(sd)'*0.2
display $caliper 

*1) CREATE PSCORE CONTROLS3
xi: pscore Earthquake $Controls3 if Miss!=1, pscore(psGeneralTrust_nr3) blockid(mbGeneralTrust_nr3) logit detail

*2) ****NO REPLACEMENT
psmatch2 Earthquake, outcome(GeneralTrust_nr) pscore(psGeneralTrust_nr3) noreplacement ate // 

*3) WITH REPLACEMENT, 4 NEIGHBORS
gen regscore3 = ln(psGeneralTrust_nr3/(1-psGeneralTrust_nr3))  // ols function of the pscore
sum regscore3
*return list
global caliper =  `r(sd)'*0.2
display $caliper 

*1) CREATE PSCORE CONTROLS4
xi: pscore Earthquake $Controls4 if Miss!=1, pscore(psGeneralTrust_nr4) blockid(mbGeneralTrust_nr4) logit detail

*2) ****NO REPLACEMENT
psmatch2 Earthquake, outcome(GeneralTrust_nr) pscore(psGeneralTrust_nr4) noreplacement ate // 

*3) WITH REPLACEMENT, 4 NEIGHBORS
gen regscore4 = ln(psGeneralTrust_nr4/(1-psGeneralTrust_nr4))  // ols function of the pscore
sum regscore4
*return list
global caliper =  `r(sd)'*0.2
display $caliper 

*1) CREATE PSCORE CONTROLS5
xi: pscore Earthquake $Controls5 if Miss!=1, pscore(psGeneralTrust_nr5) blockid(mbGeneralTrust_nr5) logit detail

*2) ****NO REPLACEMENT
psmatch2 Earthquake, outcome(GeneralTrust_nr) pscore(psGeneralTrust_nr5) noreplacement ate // 

*3) WITH REPLACEMENT, 4 NEIGHBORS
gen regscore5 = ln(psGeneralTrust_nr5/(1-psGeneralTrust_nr5))  // ols function of the pscore
sum regscore5
*return list
global caliper =  `r(sd)'*0.2
display $caliper 

*1) CREATE PSCORE CONTROLS6
xi: pscore Earthquake $Controls6 if Miss!=1, pscore(psGeneralTrust_nr6) blockid(mbGeneralTrust_nr6) logit detail

*2) ****NO REPLACEMENT
psmatch2 Earthquake, outcome(GeneralTrust_nr) pscore(psGeneralTrust_nr6) noreplacement ate // 

*3) WITH REPLACEMENT, 4 NEIGHBORS
gen regscore6 = ln(psGeneralTrust_nr6/(1-psGeneralTrust_nr6))  // ols function of the pscore
sum regscore6
*return list
global caliper =  `r(sd)'*0.2
display $caliper 

psmatch2 Earthquake , kernel  pscore(psGeneralTrust_nr1) outcome(GeneralTrust_nr) ate ties 
set seed 1234
	 
bs "psmatch2 Earthquake $Controls1, kernel outcome(GeneralTrust_nr) ate ties " "r(ate)", reps(500) dots 
mat T[1,7] = e(b)
mat T[1,8] = e(se)
mat list T

psmatch2 Earthquake , kernel  pscore(psGeneralTrust_nr2) outcome(GeneralTrust_nr) ate ties 
set seed 1234
	 
bs "psmatch2 Earthquake $Controls2, kernel outcome(GeneralTrust_nr) ate ties " "r(ate)", reps(500) dots 
mat T[3,7] = e(b)
mat T[3,8] = e(se)
mat list T

psmatch2 Earthquake , kernel  pscore(psGeneralTrust_nr3) outcome(GeneralTrust_nr) ate ties 
set seed 1234
	 
bs "psmatch2 Earthquake $Controls3, kernel outcome(GeneralTrust_nr) ate ties " "r(ate)", reps(500) dots 
mat T[5,7] = e(b)
mat T[5,8] = e(se)
mat list T

psmatch2 Earthquake , kernel  pscore(psGeneralTrust_nr4) outcome(GeneralTrust_nr) ate ties 
set seed 1234
	 
bs "psmatch2 Earthquake $Controls4, kernel outcome(GeneralTrust_nr) ate ties " "r(ate)", reps(500) dots 
mat T[7,7] = e(b)
mat T[7,8] = e(se)

mat list T

psmatch2 Earthquake , kernel  pscore(psGeneralTrust_nr5) outcome(GeneralTrust_nr) ate ties 
set seed 1234
	 
bs "psmatch2 Earthquake $Controls5, kernel outcome(GeneralTrust_nr) ate ties " "r(ate)", reps(500) dots 
mat T[9,7] = e(b)
mat T[9,8] = e(se)

mat list T

psmatch2 Earthquake , kernel  pscore(psGeneralTrust_nr6) outcome(GeneralTrust_nr) ate ties 
set seed 1234
	 
bs "psmatch2 Earthquake $Controls6, kernel outcome(GeneralTrust_nr) ate ties " "r(ate)", reps(500) dots 
mat T[11,7] = e(b)
mat T[11,8] = e(se)
mat list T

matrix stars = J(12,12,0)
matrix list stars

forvalues j = 1/12 {
forvalues k = 2(2)8 {
matrix stars[`j',`k'-1] = (abs(T[`j',`k'-1]/T[`j',`k']) > 1.645 ) + (abs(T[`j',`k'-1]/T[`j',`k']) > 1.96) + (abs(T[`j',`k'-1]/T[`j',`k']) >  2.58) 
}
}

matrix list stars

forvalues j = 1/12 {
forvalues k = 9(2)11 {
	matrix stars[`j',`k'] = 0
}
}

forvalues j = 2(2)12 {
forvalues k = 3(2)7 {
	matrix stars[`j',`k'] = 0
}
}

matrix list stars

mat rownames T = "Model (1)" "POmean - Control" "Model (2)" "POmean - Control" "Model (3)" "POmean - Control" "Model (4)" "POmean - Control" "Model (5)" "POmean - Control" "Model (6)" "POmean - Control"

frmttable using "$Folder/output/Table_F1_quake", tex statmat(T) substat(1) sdec(3) ///
 ctitle("" "IPWRA" "NNM(4)" "PSM(4 N)"  "Kernel")  annotate(stars) asymbol(*,**,***) replace
}

*=============================================================================================
**#Table G.1: Average Time and Distance between the Closest Distribution Center and Households
*=============================================================================================

if 1==0{	
mat T = J(2,7,.)

sum MinTime1 if DC_Objective==1 & Earthquake==0   & City==1, detail
mat T[1,1] = r(mean)
mat T[1,2] = r(sd)
mat T[1,3] = r(min)
mat T[1,4] = r(p25)
mat T[1,5] = r(p50)
mat T[1,6] = r(p75)
mat T[1,7] = r(max)

sum WALKdistance1 if DC_Objective==1 & Earthquake==0   & City==1, detail
mat T[2,1] = r(mean)/1000
mat T[2,2] = r(sd)/1000
mat T[2,3] = r(min)/1000
mat T[2,4] = r(p25)/1000
mat T[2,5] = r(p50)/1000
mat T[2,6] = r(p75)/1000
mat T[2,7] = r(max)/1000

matlist T

mat rownames T = "Walking time (minutes)" "Distance (Km)"

frmttable using "$Folder/output/Table_G1_quake",  statmat(T) tex varlabels replace ///
	ctitle("", Mean, S.D., Min, "p25", Median, p75, Max) sdec(2)
}
		
*======================================================
**# Table G.2 Placebo regression
*======================================================
if 1==0{
svy: reg GeneralTrust_nr i.DC_Objective $Controls1 if City==1 & Earthquake==0
est sto m1 
svy: reg GeneralTrust_nr i.DC_Objective $Controls2 if City==1 & Earthquake==0
est sto m2
svy: reg GeneralTrust_nr i.DC_Objective $Controls3 if City==1 & Earthquake==0
est sto m3
svy: reg GeneralTrust_nr i.DC_Objective $Controls4 if City==1 & Earthquake==0
est sto m4
svy: reg GeneralTrust_nr i.DC_Objective $Controls5 if City==1 & Earthquake==0
est sto m5
svy: reg GeneralTrust_nr i.DC_Objective $Controls6 if City==1 & Earthquake==0
est sto m6

esttab m1 m2 m3 m4 m5 m6 using "$Folder/output/Table_G2_quake.tex", ///
	coeflabels($labels) refcat(1.DC_Objective   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace keep(1.DC_Objective _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///	
	stats(N,layout("\multicolumn{1}{c}{@}") labels("Observations") f(0)) ///
	title(Marginal Effects of Earthquake on General Political Trust\label{tab:earthquaketrust})  ///
	nonotes ///
	addnote("Clustered standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}	
	

*======================================================
**#Table H.1: Effect of the Earthquake on other outcome variables
*======================================================

**Show that general political trust falls, but not for family members or social trust. 

if 1==1{
foreach var in FamilyPromises FamilyCompliance Family_trust Social_trust Personal_income Currently_working {
svy: reg `var' i.Earthquake $Controls6
est sto `var'
}

esttab FamilyPromises FamilyCompliance  Family_trust Social_trust Personal_income Currently_working using "$Folder/output/Table_H1_quake.tex", ///
	coeflabels($labels) refcat(1.Earthquake   "\vspace{-6mm}", nolabel) star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label replace keep(1.Earthquake _cons Sex Age  Education Adults_hh Children_hh PRI_pre PRD_pre Wealth_pre Social_trust_pre Risk_pre Patience_pre) ///
	stats(N r2,  labels(`"Observations"' `"R-squared"', prefix(\addlinespace[3pt])) fmt( %10.0fc %10.3fc 0 0) ) ///
	title(Effect of the Earthquake on other outcome variables - Trust \label{tab:earthquaketrust})  ///
	nonotes ///
	addnote("Standard errors are in parentheses. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$. The average levels of political affiliation, risk aversion, patience, and social trust are calculated at the municipality level before the earthquake.")
}

